﻿#include "TaskTray.h"

TaskTray::TaskTray(HWND hwnd, HMENU hmenu):_hwnd(hwnd), _hMenu(hmenu)
{
    LPCWCHAR tip = L"中";
    this->_lpData.cbSize = sizeof(this->_lpData);
    this->_lpData.hWnd = this->_hwnd;
    wcsncpy_s(this->_lpData.szTip, tip, sizeof(tip));
    this->_lpData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    this->_lpData.uID = 0;
    this->_lpData.uCallbackMessage = WM_RBUTTONUP | WM_MOUSEHOVER | WM_LBUTTONUP;
    this->_lpData.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    Shell_NotifyIconW(NIM_ADD, &this->_lpData);
}

TaskTray::~TaskTray()
{
    Shell_NotifyIconW(NIM_DELETE, &this->_lpData);
}

void TaskTray::dealTaskBarMessage(HWND handle, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (lParam)
    {
    case WM_RBUTTONUP:
        POINT pt;
        GetCursorPos(&pt);
        SetForegroundWindow(this->_hwnd); // 需要先设置当前窗口为系统活动窗口，之后，系统就会自动设置响应的操作，比如自动隐藏之类的。
        TrackPopupMenuEx(this->_hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, this->_hwnd, NULL);
        break;
    }
}
